//
//  ModuleBundle.m
//  Common
//
//  Created by yangke on 2017/9/17.
//  Copyright © 2017年 jackie@youzan. All rights reserved.
//

#import "ModuleBundle.h"

@implementation ModuleBundle

/*
+ (NSBundle *)resourceBundle {
    NSString *name = [self.class classSymbol];
    NSBundle *resourceBundle = [self.class resourceBundleWithName:name];
    NSAssert(resourceBundle, @"注意: 没有找到此 bundle !!! ");
    return resourceBundle;
}

+ (NSBundle *)containnerBundle {
    NSString *name = [self.class classSymbol];

    if (name.length == 0) {
        return nil;
    }

    NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
    // NSLog(@"%@",info);
    if ([name isEqualToString:info[@"CFBundleName"]]) {
        return [NSBundle mainBundle];
    }

    if ([name isEqualToString:@"UIKitCore"]) {
        return [NSBundle mainBundle];
    }

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *mainPath = mainBundle.bundlePath;
    NSString *containnerPath = [mainPath stringByAppendingFormat:@"/Frameworks/%@.framework", name];
    NSBundle *containnerBundle = [NSBundle bundleWithPath:containnerPath];
    // NSBundle *containnerBundle = [NSBundle bundleForClass:NSClassFromString(bundleName)];

    NSAssert(containnerBundle, @"注意: 没有找到此 bundle !!! ");
    return containnerBundle;
}
*/

+ (NSBundle *)bundleWithName:(NSString *)bundleName {
    if (bundleName.length == 0) {
        return nil;
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:bundleName ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    NSAssert(bundle, @"注意: 没有找到此 bundle !!! ");
    return bundle;
}

+ (NSBundle *)bundle {
    return [NSBundle mainBundle];
}

+ (UIView *)viewWithXibFileName:(NSString *)fileName {
    UIView *view = [self viewWithXibFileName:fileName inBundle:[self.class bundle]];
    NSAssert(view, @"注意: 没有找到此 view !!! ");
    return view;
}

+ (UIImage *)imageNamed:(NSString *)imageName {
    UIImage *image = [self imageNamed:imageName inBundle:[self.class bundle]];
    NSAssert(image, @"注意: 没有找到名为 %@ 的 image !!! ", imageName);
    return image;
}

+ (UIStoryboard *)storyboardWithName:(NSString *)storyboardName {
    UIStoryboard *storyboard = [self storyboardWithName:storyboardName inBundle:[self.class bundle]];
    NSAssert(storyboard, @"注意: 没有找到此 storyboard !!! ");
    return storyboard;
}

+ (UINib *)nibWithName:(NSString *)nibName {
    UINib *nib = [self nibWithNibName:nibName inBundle:[self.class bundle]];
    NSAssert(nib, @"注意: 没有找到此 nib/xib !!! ");
    return nib;
}

// 绑定 xib 方法
/*

 // 绑定 xib
 // [[ModuleBundle nibWithName:NSStringFromClass(self.class)] instantiateWithOwner:self options:nil];
 // [[[self class] nib] instantiateWithOwner:self options:nil];

 // 获取 xib
 + (UINib *)nib {
 return [UINib nibWithNibName:NSStringFromClass([JSQMessagesViewController class]) bundle:[NSBundle bundleForClass:[JSQMessagesViewController class]]];
 }

 // 实例化
 + (instancetype)messagesViewController {
 return [[[self class] alloc] initWithNibName:NSStringFromClass([JSQMessagesViewController class]) bundle:[NSBundle bundleForClass:[JSQMessagesViewController class]]];
 }
 */

#pragma mark - private

+ (NSBundle *)resourceBundleWithName:(NSString *)bundleName {
    if (bundleName.length == 0) {
        return nil;
    }

    NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
    // NSLog(@"%@",info);
    if ([bundleName isEqualToString:info[@"CFBundleName"]]) {
        return [NSBundle mainBundle];
    }

    if ([bundleName isEqualToString:@"UIKitCore"]) {
        return [NSBundle mainBundle];
    }

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *mainPath = mainBundle.bundlePath;
    NSString *containnerPath = [mainPath stringByAppendingFormat:@"/Frameworks/%@.framework", bundleName];
    NSBundle *containnerBundle = [NSBundle bundleWithPath:containnerPath];
    // NSBundle *containnerBundle = [NSBundle bundleForClass:NSClassFromString(bundleName)];

    NSString *resourcePath = [containnerBundle pathForResource:bundleName ofType:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithPath:resourcePath];
    NSAssert(resourceBundle, @"注意: 没有找到此 bundle !!! ");
    return resourceBundle;
}

// 获取当前方法的调用者
+ (NSString *)classSymbol {
    NSArray *syms = [NSThread callStackSymbols];
    // NSLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:1]);

    if (syms.count < 3) {
        return nil;
    }

    NSString *symsString = [syms objectAtIndex:2];
    NSArray *arr = [symsString componentsSeparatedByString:@" "];
    if (arr.count < 4) {
        return nil;
    }

    return arr[3];
}

+ (UIImage *)imageNamed:(NSString *)imageName inBundle:(NSBundle *)bundle {
    if (imageName.length == 0 || !bundle) {
        return nil;
    }
    return [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
}

+ (UIImage *)imageNamed:(NSString *)imageName bundleName:(NSString *)bundleName {
    return [self imageNamed:imageName inBundle:[self bundleWithName:bundleName]];
}

+ (UIView *)viewWithXibFileName:(NSString *)fileName inBundle:(NSBundle *)bundle {
    if (fileName.length == 0 || !bundle) {
        return nil;
    }
    //如果没有国际化，则直接去相应内容下的文件
    UIView *xibView = [[bundle loadNibNamed:fileName owner:nil options:nil] lastObject];
    if (!xibView) {
        //文件国际化之后，所有的bundle的文件资源都在base的目录下
        xibView = [[[NSBundle bundleWithPath:[bundle pathForResource:@"Base" ofType:@"lproj"]] loadNibNamed:fileName owner:nil options:nil] lastObject];
    }
    return xibView;
}

+ (UIView *)viewWithXibFileName:(NSString *)fileName bundleName:(NSString *)bundleName {
    return [self viewWithXibFileName:fileName inBundle:[self bundleWithName:bundleName]];
}

+ (UIStoryboard *)storyboardWithName:(NSString *)storyboardName inBundle:(NSBundle *)bundle {
    if (storyboardName.length == 0 || !bundle) {
        return nil;
    }
    return [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
}

+ (UIStoryboard *)storyboardWithName:(NSString *)storyboardName bundleName:(NSString *)bundleName {
    return [self storyboardWithName:storyboardName inBundle:[self bundleWithName:bundleName]];
}

+ (UINib *)nibWithNibName:(NSString *)nibName inBundle:(NSBundle *)bundle {
    if (nibName.length == 0 || !bundle) {
        return nil;
    }
    return [UINib nibWithNibName:nibName bundle:bundle];
}

@end
